<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:if test="${article==null }">
	<h1 class="text-center G-MarginTop150">
		<span class="icon-frown-o"></span> 无法加载此页面
	</h1>
</c:if>
<c:if test="${article!=null }">
	<input type="hidden" id="article_id" value="${article.articleId }" />
	<div class="line-big">
		<div class="xl1"></div>
		<div class="xl10">
			<div class="panel border-back" id="write_article_info_div">
				<div class="panel-body text-center">
					<h1>
						<strong>修改文章--${article.articleName }</strong>
					</h1>
				</div>
				<div class="panel-foot bg-white border-back">
					<div class="line-big">
						<div class="xl12">
							<div class="label">
								<label for="article_name">*文章名称(4-50个字)</label>
							</div>
							<div class="field">
								<input type="text" class="input" id="article_name" value="${article.articleName }" data-regex="^\S.{2,48}\S$" />
							</div>
						</div>
					</div>
					<div class="line-big margin-big-top">
						<div class="xl12">
							<div class="label">
								<label for="article_tips">文章导读(4-200字)</label>
							</div>
							<div class="field">
								<input type="text" class="input" id="article_tips" value="${article.articleTips }" data-regex="(^\S.{2,198}\S$)|(^\S{0}$)" />
							</div>
						</div>
					</div>
					<div class="line-big margin-big-top">
						<div class="xl12">
							<div class="label">
								<label for="article_detail">*文章内容</label>
							</div>
							<div class="field">
								<!-- <script type="text/plain" id="article_detail" name=""></script> -->
								<textarea name="content" class="article_detail_textarea" id="article_detail"></textarea>
							</div>
						</div>
					</div>
				</div>
				<div class="panel-foot bg-white">
					<div class="line-big">
						<div class="xl4"></div>
						<div class="xl4 text-center">
							<button class="button border-blue" id="submit_article_button">提交文章信息</button>
						</div>
						<div class="xl4"></div>
					</div>
				</div>
			</div>
			<div id="add_success_div" class="G-MarginTop100" style="display: none;">
				<div class="line-big">
					<div class="xl3"></div>
					<div class="xl6 text-center">
						<h1 class="text-center">
							<span class="icon-check-circle text-yellow"></span> 修改文章"<span id="article_name_success"></span>"成功了。
						</h1>
						<br />
						<p>
							<button class="button bg-blue click_go_to_nav_button" data-trigger-id="#sub_add_article">
								<span class="icon-plus-circle"></span> 添加文章
							</button>
							<button class="button bg-green click_go_to_nav_button" data-trigger-id="#sub_see_released_article">
								<span class="icon-cloud-upload"></span> 去查看文章
							</button>
						</p>
					</div>
					<div class="xl3"></div>
				</div>
			</div>
		</div>
		<div class="xl1"></div>
	</div>
	<div style="display: none" id="time_temp_div"></div>
	<div style="display: none" id="article_detail_temp_div">${article.articleDetail }</div>
</c:if>
<script type="text/javascript">
	$(function()
	{
		var myDate = new Date();
		$("#time_temp_div").html(myDate.getHours() + "-" + myDate.getMinutes() + "-" + myDate.getSeconds() + "-" + myDate.getMilliseconds());
		$(".article_detail_textarea").attr("id", "article_detail" + $("#time_temp_div").html());
		var option_article_detail =
		{
			initialContent : $("#article_detail_temp_div").html(),//初始化编辑器的内容
			//textarea:'editorValue',//设置提交时编辑器内容的名字
			initialFrameWidth : "100%", //初始化编辑器宽度,默认1000，可以设置成百分比
			initialFrameHeight : 500, //初始化编辑器高度,默认320
			autoClearinitialContent : false,//是否自动清除编辑器初始内容
			maxImageSideLength : 900
		};

		var article_detail_editor = UE.getEditor($(".article_detail_textarea").attr("id"), option_article_detail);

		$("#submit_article_button").click(function()
		{
			var article_id = $("#article_id").val();
			var article_name = $("#article_name").val();
			var article_tips = $("#article_tips").val();

			var article_detail = article_detail_editor.getContent();

			if (article_name.length <= 0 || article_detail.length <= 0)
			{
				alert("标有*的输入项都是必填项.")
				return false;
			}

			var tips = "";
			var tips_number = 1;
			$.each([ $("#article_name"), $("#article_tips") ], function(i, n)
			{
				if (new RegExp(n.attr("data-regex")).test(n.val()) == false)
				{
					tips += tips_number++ + " . \"" + n.val() + "\" 不满足输入要求;\n";
				}
			});
			if (tips.length > 0)
			{
				alert(tips + "\n请检查一下重新输入.");
				return false;
			}

			var submit_article_button_html = $("#submit_article_button").html();
			$.ajax(
			{
				url : "admin/m_update_article_do",
				type : "POST",
				dataType : "json",
				beforeSend : function(XMLHttpRequest)
				{
					$("#submit_article_button").attr("disabled", "disabled");
					$("#submit_article_button").html("正在提交商品信息中...");
				},
				data :
				{
					'article.articleId' : article_id,
					'article.articleName' : article_name,
					'article.articleTips' : article_tips,
					'article.articleDetail' : article_detail
				},
				success : function(json, textStatus)
				{
					if (json.success == true)
					{
						$("#article_name_success").html(article_name);
						$("#write_article_info_div").slideUp();
						$("#add_success_div").slideDown(function()
						{
							$(".admin").animate(
							{
								scrollTop : 0
							}, 0);
						});

					}
					else
					{
						alert(json.info);
					}
				},
				error : function(XMLHttpRequest, textStatus, errorThrown)
				{
					alert("提交失败，请仔细检查您的输入是否满足要求。");
				},
				complete : function(XMLHttpRequest, textStatus)
				{
					$("#submit_article_button").removeAttr("disabled");
					$("#submit_article_button").html(submit_article_button_html);
				}
			});
		});

	});
</script>